from functools import cache
from typing import *




class Solution:

    def numFactoredBinaryTrees(self, arr: List[int]) -> int:
        h = dict()
        for i in range(len(arr)):
            h[arr[i]] = i
            
        @cache
        def f(index):
            ans, v = 1, arr[index]
            for i in range(len(arr)):
                if v % arr[i] == 0 and v // arr[i] in h:
                    ans += f(i) * f(h[v // arr[i]])
            return ans

        ans = 0
        for i in range(len(arr)):
            ans += f(i)

        return ans % (10**9 + 7)


s = Solution()
s.numFactoredBinaryTrees([2, 4])
